var app = getApp();
Page({
    data:{
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        var zfid = options.zfid ? options.zfid : '';

        self.setData({
            options : options
        });

        app.com.auth(function(user){
            var postdata = {
                op : 'info',
                id : options.id,
                zfid : zfid,
            };
            app.com.http('group','POST',postdata,0,false,'','',function(res){
                if(!res.data.errno){

                    var needGroupList = res.data.data.grouplist;
                    needGroupList.length += res.data.data.group.dismem*1;
                    needGroupList.length += res.data.data.group.falsemem*1;
                    self.setData({
                        Pagetheme : res.data.data.params.style,
                        good : res.data.data.good,
                        group : res.data.data.group,
                        userinfo : res.data.data.userinfo,
                        grouplist : needGroupList,
                        inited : true,
                        params : res.data.data.params,
                        sharedata : res.data.data.sharedata,
                    });
                    
                    if(self.data.group.status == 3){
                        self.endTime();
                    }

                    wx.setNavigationBarTitle({
                      title:  res.data.data.params.title ? res.data.data.params.title : '拼单',
                    });

                }else{
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(options.zfid);
                    });
                }
            })
            
        },self);

        wx.setNavigationBarTitle({
          title:  '团详情',
        });
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.data.isdown = true;
        this.onLoad( this.data.options );
        this.data.isdown = false;
        wx.stopPullDownRefresh();
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onShareTimeline(){
        return {
            title: this.data.sharedata.title,
            query: this.data.sharedata.query,
            imageUrl : this.data.sharedata.img,
        }
    },
    endTime(){
        var self = this;
        if( self.data.timer ) return false;

        self.data.timer = setInterval(function(){
            
            var date = new Date();
            var time = date.getTime();

            if(self.data.group.status == 3){
                var endTime = self.data.group.end + '000';
                
                var lag = (endTime*1 - time);
                if(lag > 0){
                    var second = Math.floor(lag/1000%60);     
                    var minite = Math.floor(lag/1000/60%60);
                    var hour = Math.floor(lag/1000/60/60%24);
                    var day = Math.floor(lag/1000/60/60/24);
                }else{
                    var second = 0;
                    var minite = 0;
                    var hour = 0;
                    var day = 0;
                }
                self.setData({
                    Countdown : {day:day,hour:hour,minute:minite,second:second}
                });
            }
        },1000);

    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    joinit(){
        var self = this;
        /*if( this.data.good.isinsever == 0 ){
            app.com.toast('不在销售范围内');return false;
        }
        if( this.data.good.isintime == 0 ){
            app.com.toast('不在下单时间内');return false;
        }*/
        // 0正常购买 1拼团 2参团
        if( self.data.good.isrule == 1 ){
            this.selectComponent('#cartitem').join();
        }else{
            wx.setStorageSync('zfssbuy',{'gid':this.data.good.id,type:2,groupid:self.data.group.id});
            var tourl = '/zofui_sales/order/confirm';
            wx.navigateTo({
              url: tourl
            });
        }
    },
    setRule(e){
        wx.setStorageSync('zfssbuy',{'gid':this.data.good.id,type:2,groupid:this.data.group.id,mapid:e.detail});
        var tourl = '/zofui_sales/order/confirm';
        wx.navigateTo({
          url: tourl
        });
    },
    showPicMore(){
        var self = this;
        self.setData({
            showGroup : !self.data.showGroup
        })
    },
    updateUserInfo : function(e){
        var self = this;
        app.com.getUserInfo(function(res){
            self.setData({
                showuserbtn:false,
            });
        },false,e.detail);
    },
    hideauth(){
        this.setData({
            showuserbtn:false,
        });
    },
})